﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using AppCommon;
using Controller;
using Model;

public partial class ACPanel_NodeMedia_AddEditPhotos : System.Web.UI.Page
{
    public static string _id;
    public static SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["defaultConnection"].ConnectionString);    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {            
            var id = Request.QueryString["Id"];
            if (id != null)
            {
                id = id.Replace("'", "''");
                _id = id;
                BindData(_id);
                FillGrid("NgayCapNhat", false);
            }            
        }
    }

    public void BindData(string id)
    {
        var controller = new VYC22Controller();
        var model = controller.GetLimitAlbumForModule(1, id);
        if (model.Count > 0)
        {
            tbTieude.Text = model[0].TieuDe;
            tbNoidung.Text = Server.HtmlDecode(model[0].NoiDung);
            tbTags.Text = model[0].TomTat;
            cbTrangthai.Checked = Convert.ToBoolean(model[0].TrangThai);
        }
        else
        {
            
        }
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        // StringBuilder sb = new StringBuilder();
        for (int i = 0; i < Request.Files.Count; i++)
        {
            HttpPostedFile postedFile = Request.Files[i];
            if (postedFile.ContentLength <= 0) continue;
            var fileName = Path.GetFileName(postedFile.FileName);
            var ext = Path.GetExtension(postedFile.FileName);

            var pathAnhbia = Server.MapPath(Constants.UPLOAD_ALBUMANH + _id + "/");
            if (!Directory.Exists(pathAnhbia))
            {
                Directory.CreateDirectory(pathAnhbia);
            }
            //if (File.Exists(Server.MapPath(Constants.UPLOAD_ALBUMANH + _id + "/") + fileName))
            //{
            //    fileName = Common.ConvertDateTimetoString() + fileName;
            //    postedFile.SaveAs(Server.MapPath(Constants.UPLOAD_ALBUMANH + _id + "/") + fileName);
            //}
            //else
            //{                
            fileName = "Img-" + Common.ConvertDateTimetoString() + ext;
            postedFile.SaveAs(Server.MapPath(Constants.UPLOAD_ALBUMANH + _id + "/") + fileName);
            //}
            // sb.Append(fileName + ",");

            var controller = new VYC22Controller();
            var model = new ModelVYC22();

            model.IdAlbumAnh = _id;
            model.TomTat = tbTags.Text;
            model.AnhDaiDien = fileName;
            model.TrangThai = Convert.ToInt32(cbTrangthai.Checked);
            model.TieuDe = tbTieude.Text;
            model.LuotXem = 0;
            model.NoiDung = Server.HtmlEncode(tbNoidung.Text);

            var r = controller.AddData(model);

        }                        

        Response.Redirect("~/ACPanel/NodeMedia/QLAlbumAnh.aspx");
    }

//    protected void btCapnhat_OnClick(object sender, EventArgs e)
//    {
//        // Thuc hien cap nhat khi du lieu da co roi
//    }
    
    /// <summary>
    /// Method : FillGrid 
    /// Use : Bind data to gridview
    /// </summary>
    /// <param name="SortExpression"></param>
    /// <param name="IsSorting"></param>
    public void FillGrid(string SortExpression, bool IsSorting)
    {
        var dt = new DataTable();

        // Lay chuoi ket noi CSDL
        Connection.Open();
        var query = "SELECT * FROM [VYC-22] WHERE IdAlbumAnh like '" + _id + "' ORDER BY NgayCapNhat DESC";
        var dataAdapter = new SqlDataAdapter(query, Connection);
        dataAdapter.Fill(dt);
        Connection.Close(); // dong ket noi       

        var dv = dt.DefaultView;
        if (ViewState["SortExpression"] != null && ViewState["SortDirection"] != null)
        {
            if (IsSorting)
            {
                if (ViewState["SortExpression"].ToString() == SortExpression)
                {
                    if (ViewState["SortDirection"].ToString() == "ASC")
                        ViewState["SortDirection"] = "DESC";
                    else
                        ViewState["SortDirection"] = "ASC";
                }
                else
                {
                    ViewState["SortExpression"] = SortExpression;
                    ViewState["SortDirection"] = "ASC";
                }
            }
        }
        else
        {
            ViewState["SortExpression"] = SortExpression;
            ViewState["SortDirection"] = "ASC";
        }

        dv.Sort = ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        gv.DataSource = dv.ToTable();
        gv.DataBind();
    }

    /// <summary>
    /// Event OnPageIndexChanging="gv_PageIndexChanging" on GridView
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv.PageIndex = e.NewPageIndex;
        FillGrid(ViewState["SortExpression"].ToString(), false);
    }

    /// <summary>
    /// Event OnSorting="gv_Sorting" on GridView
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gv_Sorting(object sender, GridViewSortEventArgs e)
    {
        FillGrid(e.SortExpression, true);
    }
}